from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import joinedload

from src.infrastructure.db.models import Server


async def get_server(session: AsyncSession, server_id: int, joined: bool = False) -> Server:
    q = select(Server).where(Server.id == server_id)

    if joined:
        q = q.options(
            joinedload(Server.provider),
            joinedload(Server.categories),
            joinedload(Server.country),
            joinedload(Server.city),
        )

    res = await session.execute(q)

    return res.scalar()
